package com.epoint.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class JdbcRest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/getInspectionList")
    public Map<String, Object> getInspectionList(@RequestParam("params")JSONObject params) throws JSONException {
        String sql = " select * from spc_inspection where 1=1 ";

        List<Map<String, Object>> list = null;
        if(params != null && params.has("id") && !StringUtils.isEmpty(params.getString("id"))){
            list = jdbcTemplate.queryForList(sql + " AND rowguid=? ", params.getString("id"));
        }
        else{
            list = jdbcTemplate.queryForList(sql);
        }

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> custom = new HashMap<>();

        custom.put("inspectionlist", list);
        result.put("status", "200");
        result.put("custom", custom);
        return result;
    }
}
